/* ----------------------------------------------------------------------------------------------------------------------------------------------- /

GMap - 地图显示主类
修改许可权限 - 江利峰 

< --- Public Vars ------------------------------------------------------------------------------------------------------------------------------ >

< --- Public Functions ------------------------------------------------------------------------------------------------------------------------ >

※ 构造函数
public function GMap()

/ ----------------------------------------------------------------------------------------------------------------------------------------------- */
/**
 *
 */
package editor.map{
	import com.greensock.TweenNano;
	import com.zero.framework.interfaces.INotification;
	import com.zero.framework.interfaces.INotifyResponder;
	import com.zero.framework.observer.Facade;
	
	import editor.Notification.M2VNotification;
	import editor.Notification.V2MNotification;
	import editor.common.AppFacade;
	import editor.common.Common;
	import editor.common.CursorManager;
	import editor.manager.MapManger;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	
	
	public class GMap extends Sprite implements INotifyResponder{

		
		private var _tileLayer:TileLayer;
		private var _backgroundLayer:BackgroundLayer;
		private var _npcLayer:NpcLayer;
		
		public var mapName:String = "未命名";
		public var mapId:String = "无id";
		
		
		// 构造函数
		public function GMap()
		{
			AppFacade.getIntance().registerResponder(this);
			
			_backgroundLayer = new BackgroundLayer();
			addChild(_backgroundLayer);
			
			_tileLayer = new TileLayer();
			addChild(_tileLayer);
			
			_npcLayer = new NpcLayer();
			addChild(_npcLayer);
			
			MapManger.getInstance().gmap = this;
			MapManger.getInstance().tileLayer = _tileLayer;
			MapManger.getInstance().backgroundLayer = _backgroundLayer;
			MapManger.getInstance().npcLayer = _npcLayer;
			
			addEventListener(MouseEvent.RIGHT_MOUSE_DOWN, onTileLayerDown);
			addEventListener(MouseEvent.RIGHT_MOUSE_UP, onTileLayerUp);
			_backgroundLayer.addEventListener(MouseEvent.MOUSE_DOWN, _tileLayer.onMouseDown);
			_backgroundLayer.addEventListener(MouseEvent.MOUSE_UP, _tileLayer.onMouseUp);
			addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		public function get notificationList():Array
		{
			return [
				V2MNotification.SET_MAP_VIEW
			];
		}
		
		public function notifyHandler(notification:INotification):void
		{
			var data:Object = notification.getBody();
			switch(notification.getName())
			{
				case V2MNotification.SET_MAP_VIEW:
					setMapView(data.vx, data.vy);
					break;
				default:
					new Error(Facade.ERROR_NOTIFICATION_NOT_SEEN);
					break;
			}
		}
		
		private function onEnterFrame(e:Event):void
		{
			Common.ui.layerControllerWindow.updateXY(mouseX, mouseY);
		}
		
		
		private function onMouseUp(evt:MouseEvent):void
		{
			switch(Common.cursor.state)
			{
				case CursorManager.STATE_ADD_NPC:
					_npcLayer.addNpc("");
					break;
				case CursorManager.STATE_ADD_IN:
					_npcLayer.addInGate();
					break;
				case CursorManager.STATE_ADD_LINK:
					_npcLayer.addLinkGate();
					break;
				case CursorManager.STATE_ADD_PRE:
					_npcLayer.addPreGate();
					break;
				case CursorManager.STATE_ADD_NEXT:
					_npcLayer.addNextGate();
					break;
				default:
					break;
			}
			Common.cursor.state = CursorManager.STATE_NONE;
		}
		
		private function onTileLayerDown(evt:MouseEvent):void
		{
			if(contains(_tileLayer))
				removeChild(_tileLayer);
			startDrag();
		}
		
		private function onTileLayerUp(evt:MouseEvent):void
		{
			addChild(_tileLayer);
			setChildIndex(_npcLayer, numChildren - 1);
			stopDrag();
		}
		
		/**
		 * 设置地图位置 
		 */		
		private function setMapView(vx:int, vy:int):void
		{
			var nx:Number = Common.stage.stageWidth / 2  - vx;
			var ny:Number = Common.stage.stageHeight / 2 - vy;
			
			TweenNano.to(this, 0.2, {x:nx, y:ny});
		}
		//
	}
}